//
//  dayAllCell.m
//  planningV3
//
//  Created by richard_local on 20/09/13.
//  Copyright (c) 2013 ierdre.com. All rights reserved.
//

#import "dayAllCell.h"

@implementation dayAllCell
@synthesize  data;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
   // [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


-(void) reset {
    [self setNeedsDisplay];
}



//*** Drax
#define HauteurLabel 20 
-(void) drawRect: (CGRect) rect
{
   /* 
    */
    int largeur = self.bounds.size.width;
    int hauteur = self.bounds.size.height;
    UILabel * heureDeb = [[UILabel alloc] init];
   // heureDeb.frame = CGRectMake(2, hauteur-22, 100,22);
     heureDeb.frame = CGRectMake(0, hauteur-HauteurLabel-3, largeur ,HauteurLabel);
   // [heureDeb setBackgroundColor:[UIColor clearColor]];
    //NSString * hd= [NSString stringWithFormat:@"%.2f" ,self.data.heureDebut];
    [heureDeb setText: self.data.heureDebut];
    [self addSubview:heureDeb];
    
    //CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];
    
    
    UILabel * heureFin = [[UILabel alloc] init];
    [heureFin setText: self.data.heureFin];
    CGSize expectedLabelSize = [self.data.heureFin sizeWithFont:heureFin.font constrainedToSize:CGSizeMake(44, 22) lineBreakMode:heureFin.lineBreakMode];
    heureFin.frame = CGRectMake(largeur - expectedLabelSize.width, hauteur-HauteurLabel-3, 44,HauteurLabel);
  
    [heureFin setBackgroundColor:[UIColor clearColor]];
    [self addSubview:heureFin];
    
    
    //Draw zone couleur
    
    
    //NSLog (@"-- > zone %d",self.data.nombreDeZone );
    
    
    
    int cnt=self.data.nombreDeZone ;
    int x =0 ;
    int y =0;
    int xx=0;
    int yy=50;
    int indice =0;
    CGContextRef context = UIGraphicsGetCurrentContext();

    for (int i=1 ; i < cnt-1 ; i++){
        xx =  [self.data  getEndPositionTransition:i largeurCellule:self.bounds.size.width];
        
        indice =  [self.data  getEndIndiceTransition:i];
       // NSLog(@"%f %d  %d %d",self.bounds.size.width,x,xx,indice );
       // NSLog(@" %f -- %f  ",dureeJournee ,dureeIntervalle);
        
        
        // CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
        float alp = (indice *0.2);
        UIColor * redColor = [UIColor colorWithRed:0.0 green:1.0 blue:0.0 alpha:alp+0.1];

       if (indice < 0)
            redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:(-alp)+0.1];
        if (indice == 0){
            redColor = [UIColor grayColor];
        }
        CGContextSetFillColorWithColor(context, redColor.CGColor);


        CGContextFillRect(context,  CGRectMake(x,y,xx,yy-HauteurLabel));

        //info texte
        /*UILabel * indicateur = [[UILabel alloc] init];
        [indicateur  setText: [NSString stringWithFormat:@"%d",indice]];
        indicateur.frame = CGRectMake(x+(xx/2),y+3,20,20);
        [indicateur setBackgroundColor:[UIColor clearColor]];
        [self addSubview:indicateur];*/
        
        NSString *text = [NSString stringWithFormat:@"%d",indice];
        CGPoint center = CGPointMake(x+(xx/2), y+10);
        UIFont *font = [UIFont systemFontOfSize:14];
        
        CGSize stringSize = [text sizeWithFont:font];
        CGRect stringRect = CGRectMake(center.x-stringSize.width/2, center.y-stringSize.height/2, stringSize.width, stringSize.height);
        
        [[UIColor clearColor] set];
        CGContextFillRect(context, stringRect);
        
        [[UIColor blackColor] set];
        [text drawInRect:stringRect withFont:font];
        
        
        //END TEXTE info
        
        
        x = xx+x;

        
    }
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 1.0f);
    CGContextMoveToPoint(context, 0,0 );
    CGContextAddLineToPoint(context, largeur, 0);
    CGContextStrokePath(context);
    CGContextMoveToPoint(context, 0,HauteurLabel );
    CGContextAddLineToPoint(context, largeur, HauteurLabel);
    CGContextStrokePath(context);
    
    /*CGContextMoveToPoint(context, 0, hauteur );
    CGContextAddLineToPoint(context, largeur,  hauteur);
    CGContextStrokePath(context);*/
    //draw line
    
    //End draw line
    
    
    
}

@end
